<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>把js对象转换成json格式字符串</title>
    <script type="text/javascript" src="js/json2.js"></script>
    <script type="text/javascript">
        //创建一个js对象
        var jsObject=new Object();
        jsObject.name='tzlan';
        jsObject.age=13;
        jsObject.key01=[1,3,5,7];
        jsObject.key02={key06:1,key07:2};
        //采用字符串拼接
        var jsonString01="{name:"+jsObject.name+",age:"+jsObject.age+"}"
        console.log("js对象转换成json格式字符串："+jsonString01+"。");
        //json工具类
        var jsonString02=JSON.stringify(jsObject);
        console.log("js对象转换成json格式字符串："+jsonString02+"。");

        var jsObject01=[{name:'tzlan01',age:'1'},{name:'tzlan02',age:'2'},{name:'tzlan03',age:'3'}];
        var jsonString03=JSON.stringify(jsObject01);
        console.log("js对象转换成json格式字符串："+jsonString03+"。");

    </script>
</head>
<body>

</body>
</html>